<?php
class Application
{
	/* config file values */
	public $config = null;
	/* controller object */
	public $controller;
	/* controller method name */
	public $action = null;
	/* server path_info */
	public $path_info = null;
	/* array of url path_info segments */
	public $url_segments = null;

	public $layout;

	protected static
		$instances = array(),
		$current   = 'default',
		$template,
		$templateLayout = 'layout';

	public function __construct()
	{
		ControleURI::getURI();
	}

	static public function getInstance($name = null, $class = __CLASS__)
	{
		if (is_null($name))
		{
			$name = self::$current;
		}

		if (!isset(self::$instances[$name]))
		{
			throw new Exception(sprintf('The "%s" context does not exist.', $name));
		}

		return self::$instances[$name];
	}

	static public function createInstance($app, $class = __CLASS__)
	{
		$name = $app;
		self::$current = $name;
		self::$instances[$name] = new $class();

		return self::$instances[$name];
	}

	/**
	 * main method of execution
	 *
	 * @access	public
	 */
	public function main($app)
	{
		self::createInstance($app);

		$this->setupController();
		$this->setupAction();
		//$this->setupLayout();

		eval('$resultado = $this->controller->'.$this->action.'();');
		$this->startLayout($resultado);
	}

	/**
	 * setup controller
	 *
	 * @access	public
	 */
	public function setupController()
	{
		if(class_exists('Controle'.ucfirst(ControleURI::$control)))
			$controller_class = 'Controle'.ucfirst(ControleURI::$control);
		else
			$controller_class = 'ControleIndex';
		/* instantiate the controller */
		$this->controller = new $controller_class();
	}

	/**
	 * setup controller method (action) to execute
	 *
	 * @access    public
	 */
	public function setupAction()
	{
		if(isset(ControleURI::$action)) {
			if(method_exists('Controle'.ucfirst(ControleURI::$control), ControleURI::$action))
				$this->action = ControleURI::$action;
			else {
				$this->action = 'index';
				ControleURI::$action = 'index';
			}
		} else {
			$this->action = "index";
		}
	}

	public function setupLayout()
	{
		$this->layout = new View();
		$this->layout->allow_php_templates= true;
		$this->layout->template_dir = APPLICATION_PATH . '/layouts/';

		$retorno = Mensagem::getMessage();
        if($retorno){
        	$this->layout->assign("mensagem",  $retorno["mensagem"]);
            $this->layout->assign("type", $retorno["type"]);
       	}
	}

	public function startLayout($resultado)
	{
		if($resultado){
			if($resultado instanceof Smarty)
			{
				$template = (self::$template == null) ? ControleURI::$action : self::$template;

				if($resultado->templateExists($template. '.php')){
					if($resultado->exibeLayout) {
						$this->setupLayout();
						$this->layout->assign('content', $resultado->getView($template));
						echo Util::compacto($this->layout->fetch('php:'.self::getLayout().'.php'));
					} else {
						echo $resultado->getView($template);
					}
				}
				else
					throw new Exception('Não existe o template: ' . $template. '.php no modulo '. ControleURI::$module);
			}
			else
			{
				echo $resultado;
			}
		}
		else
		{
			echo $resultado;
		}
	}

	public static function setLayout($layout = 'layout')
	{
		self::$templateLayout = $layout;
	}

	public static function getLayout()
	{
		return self::$templateLayout;
	}

	public static function setTemplate($template)
	{
		self::$template = $template;
	}

	public static function getTemplate()
	{
		return self::$template;
	}
}